<?php

/**
 * This is the model class for table "tree".
 *
 * The followings are the available columns in table 'tree':
 * @property integer $id_tree
 * @property integer $id_user
 * @property integer $id_typetree
 * @property string $title
 * @property string $maintext
 * @property string $picture
 * @property integer $prize
 * @property string $province
 * @property string $date_post
 * @property string $date_update
 * @property integer $numviewer
 *
 * The followings are the available model relations:
 * @property Treetype $idTypetree
 * @property Users $idUser
 */
class Tree extends CreatimeTree
{

	/**
	 * Returns the static model of the specified AR class.
	 * @return Tree the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tree';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(' title, maintext, picture, prize, province', 'required'),
			array('prize', 'numerical', 'integerOnly'=>true),
			array('title, picture, province', 'length', 'max'=>200),
           // array('id_typetree','safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_tree, id_user, id_typetree, title, maintext, picture, prize, province, date_post, date_update, numviewer', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
			'idTypetree' => array(self::BELONGS_TO, 'Treetype', 'id_typetree'),
			'idUser' => array(self::BELONGS_TO, 'Users', 'id_user'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_tree' => 'Id Tree',
			'id_user' => 'Id User',
			'id_typetree' => 'Loại Cây',
			'title' => 'Tiêu Đề',
			'maintext' => 'Mô Tả Chi Tiết',
			'picture' => 'Ảnh Đại Diện',
			'prize' => 'Giá',
			'province' => 'Xuất Xứ',
			'date_post' => 'Ngày Đăng',
			'date_update' => 'Date Update',
			'numviewer' => 'Số Người Xem',
            'user_post'=>'Nghệ Nhân',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_tree',$this->id_tree);
		$criteria->compare('id_user',$this->id_user);
		$criteria->compare('id_typetree',$this->id_typetree);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('maintext',$this->maintext,true);
		$criteria->compare('picture',$this->picture,true);
		$criteria->compare('prize',$this->prize);
		$criteria->compare('province',$this->province,true);
		$criteria->compare('date_post',$this->date_post,true);
		$criteria->compare('date_update',$this->date_update,true);
		$criteria->compare('numviewer',$this->numviewer);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
     protected function afterValidate()
      {
        parent::afterValidate();
        $this->id_tree=$this->id_typetree;          
      }
      
     public function getUserOptions()
    {
      $usersArray = CHtml::listData($this->idUser, 'id', 'name');   
          return $usersArray;
    }
    
     public function getTypeTreeOptions()
    {
        // = CHtml::listData($this->idTypetree,'id_typetree','name');
        $TypeArray = CHtml::listData(Treetype::model()->findAll(array('order' => 'name')), 'id_typetree', 'name');
       /* $TypeArray = array();
        $Typetree = Treetype::model()->findAll(array('order' => 'name'));
        foreach($Typetree as $type):
            $TypeArray[$type['id_typetree']]=$type['name'];
        endforeach;
        return $TypeArray ;*/
        return $TypeArray;
        
    }
}